﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FF.Entity;
using FF.Method;

namespace FF
{
    public partial class FormCustomer : Form
    {
        public const int ACTION_TYPE_ADD = 1;
        public const int ACTION_TYPE_MODIFY_DELETE = 2;
        const string ID_HEADER = "KH-";
        private int actionType;
        public string userId; //选择用户姓名时保存ID
        public string userName;//选择用户时保存姓名
        private Customer customer;
        public FormCustomer()
        {
            InitializeComponent();
        }
        public FormCustomer(int actiontype)
        {
            InitializeComponent();
            actionType = actiontype;
        }
        public FormCustomer(string customerId)
        {
            InitializeComponent();
            actionType = ACTION_TYPE_MODIFY_DELETE;
            this.customer = CustomerMethod.getEntityById(customerId);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确定删除吗？", "确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (CustomerMethod.delete(textBoxId.Text) == 1)
                {
                    MessageBox.Show("删除成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("删除失败");

                }
            }
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void FormCustomer_Load(object sender, EventArgs e)
        {
            DataSet ds = SQLiteHelper.Query("select type,name from ff_customer_type");
            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "type";
            switch (actionType)
            {
                case ACTION_TYPE_ADD:
                    this.Text = "添加客户";
                    //this.textBoxId.Text = GoodMethod.generateID(ID_HEADER);
                    this.textBoxId.Enabled = false;
                    buttonDelete.Visible = false;
                    buttonOk.Text = "添加";
                    
                    comboBox1_SelectedIndexChanged(sender,e);//初始化
                    break;
                case ACTION_TYPE_MODIFY_DELETE:
                    this.Text = "修改/删除客户";
                    //this.good = GoodMethod.getGoodById(id);
                    this.textBoxId.Enabled = false;
                    this.textBoxId.Text = customer.customerId;
                    this.textBoxName.Text = customer.name;
                    textBoxAddress.Text = customer.address;
                    textBoxDiscount.Text = customer.discount;
                    richTextBoxRemark.Text = customer.remark;
                    textBoxPhone.Text = customer.phone;
                    textBoxTel.Text = customer.tel;
                    this.userId = customer.userId;
                    this.textBoxUsername.Text = customer.userName;
                    comboBox1.SelectedValue = customer.type;

                    buttonDelete.Visible = true;
                    buttonOk.Text = "修改";
                    break;
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            switch (actionType)
            {
                case ACTION_TYPE_ADD:
                    if(textBoxName.Text == "")
                    {
                        MessageBox.Show("客户名称不能为空");
                        return;
                    }
                     customer = new Customer();
                    //customer.customerId = textBoxId.Text;
                    customer.name = textBoxName.Text.Trim();
                    customer.phone = textBoxPhone.Text.Trim();
                    customer.tel = textBoxTel.Text.Trim();
                    customer.remark = richTextBoxRemark.Text;
                    customer.userName = textBoxUsername.Text.Trim();
                    customer.discount = textBoxDiscount.Text.Trim();
                    customer.address = textBoxAddress.Text.Trim();
                    if(customer.userName.Equals(userName))  //用户没有自己输入负责人姓名
                    {
                        customer.userId = this.userId;
                    }
                    customer.type = comboBox1.SelectedValue.ToString();

                    if(CustomerMethod.add(customer) == 1)
                    {
                        MessageBox.Show("添加成功");
                        this.Close();
                    }else{
                        MessageBox.Show("添加失败");
                    }
                    break;
                case ACTION_TYPE_MODIFY_DELETE:
                    this.Text = "修改/删除客户";
                    //this.good = GoodMethod.getGoodById(id);
                    if (textBoxName.Text == "")
                    {
                        MessageBox.Show("客户名称不能为空");
                        return;
                    }
                    //customer = new Customer();
                    customer.customerId = textBoxId.Text;
                    customer.name = textBoxName.Text.Trim();
                    customer.phone = textBoxPhone.Text.Trim();
                    customer.tel = textBoxTel.Text.Trim();
                    customer.remark = richTextBoxRemark.Text;
                    customer.userName = textBoxUsername.Text.Trim();
                    customer.discount = textBoxDiscount.Text.Trim();
                    customer.address = textBoxAddress.Text.Trim();
                    if (customer.userName.Equals(userName))  //用户没有自己输入负责人姓名
                    {
                        customer.userId = this.userId;
                    }
                    customer.type = comboBox1.SelectedValue.ToString();
                    if (CustomerMethod.update(customer) == 1)
                    {
                        MessageBox.Show("修改成功");
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("修改失败");
                    }
                    break;
            }
        }

        private void textBoxDiscount_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxPhone_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) )
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void textBoxTel_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar) )
            {
                MessageBox.Show("请输入数字");
                e.Handled = true;
            }
        }

        private void buttonChooseUser_Click(object sender, EventArgs e)
        {
            FormUserSelect frm = new FormUserSelect();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string type = comboBox1.SelectedValue.ToString();
            textBoxDiscount.Text = CustomerTypeMethod.getDiscountByType(type);

        }
    }
}
